package com.car.util;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.car.dal.dao.department.TgDepartmentDao;
import com.car.dal.dao.user.UserDao;
import com.car.dal.dao.user.UserinfoDao;
import com.car.dal.object.TgDepartment;
import com.car.dal.object.TgUser;
import com.car.dal.object.TgUserinfo;

/**
 * 用户相关操作辅助类
 */

public class UserUtil {

	/**
	 * 根据Cookie信息获取当前登录用户信息，如果不合法则返回null
	 * 
	 * @param cookies
	 * @return
	 */
	public static TgUser getCookieUser() {
		final Log logger = LogFactory.getLog(UserUtil.class);
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();
		Cookie[] cookies = request.getCookies();
		if (cookies == null) {
			return null;
		}
		// 以下操作判断用户是否有用户登录的cookie信息
		String cookievalue = "";
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				if (cookie.getName().equals(Constants.DOMAIN_KEY)) {
					cookievalue = cookie.getValue();
					break;
				}
				;
			}
		}
		// 判断时候存在用户cookie信息，没有则跳转到登录页面
		if (!"".equals(cookievalue)) {
			// 先得到的CookieValue进行Base64解码
			try {
				String cookieValueAfterDecode = new String(
						Base64.decode(cookievalue), "utf-8");
				// 对解码后的值进行分拆,得到一个数组,如果数组长度不为3,就是非法登陆
				String cookieValues[] = cookieValueAfterDecode
						.split(Constants.WORD_SEPARATOR);
				if (cookieValues != null && cookieValues.length == 2) {
					String loginName = cookieValues[0];
					//String token = cookieValues[1];
					UserDao userIceService = (UserDao) SpringUtil.getBean("UserDao");
					TgUser user = new TgUser();
					user.setUsername(loginName);
					// 验证登录信息
					user = userIceService.getUserInfoByType(user);
					return user;
				}
			} catch (UnsupportedEncodingException e1) {
				logger.error(e1.getMessage());
			}
		}
		return null;
	}
	

	/**
	 * 从request中获取当前登录用户信息
	 * 
	 * @return
	 */
	public static TgUser getUserFromCookies() {
		HttpServletRequest request = getRequest();
		if (request != null) {
			Object obj = request.getAttribute(Constants.USER_QER);
			if (obj != null) {
				TgUser ddbUser = (TgUser) obj;
				return ddbUser;
			}
		}
		return null;
	}

	public static HttpServletRequest getRequest() {
		return ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();
	}

	public static String getCookieValue(String keyName) {
		HttpServletRequest request = getRequest();
		Cookie[] cookies = request.getCookies();
		// 以下操作判断用户是否有用户登录的cookie信息
		Cookie findCookie = null;
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				if (cookie.getName().equals(keyName)) {
					findCookie = cookie;
					break;
				}
			}
		}
		if (findCookie != null) {
			return findCookie.getValue();
		} else {
			return null;
		}
	}



}